package com.lrkj.payfee.service.impl;

import com.lrkj.common.exception.BaseException;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.SecurityUtils;
import com.lrkj.framework.utils.NumberUtils;
import com.lrkj.framework.utils.SerialNumUtil;
import com.lrkj.payfee.domain.RoomMeter;
import com.lrkj.payfee.mapper.RoomMeterMapper;
import com.lrkj.payfee.service.ICopyRoomService;
import com.lrkj.payfee.service.IMeterKindService;
import com.lrkj.payfee.service.IRoomMeterService;
import com.lrkj.property.domain.Room;
import com.lrkj.property.service.IRoomService;
import com.lrkj.system.service.IOrgTreeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 房间仪表Service业务层处理
 *
 * @author yajun_ren
 * @date 2021-07-27
 */
@Service
public class RoomMeterServiceImpl implements IRoomMeterService {
    @Autowired
    private RoomMeterMapper roomMeterMapper;

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IOrgTreeService orgTreeService;

    @Autowired
    private ICopyRoomService copyRoomService;

    @Autowired
    private IMeterKindService iMeterKindService;

    /**
     * 查询房间仪表
     *
     * @param rometerId 房间仪表ID
     * @return 房间仪表
     */
    @Override
    public RoomMeter selectRoomMeterById(String rometerId) {
        return roomMeterMapper.selectRoomMeterById(rometerId);
    }

    /**
     * 查询房间仪表列表
     *
     * @param roomMeter 房间仪表
     * @return 房间仪表
     */
    @Override
    public List<RoomMeter> selectRoomMeterList(RoomMeter roomMeter) {
        return roomMeterMapper.selectRoomMeterList(roomMeter);
    }

    /**
     * 新增房间仪表
     *
     * @param roomMeter 房间仪表
     * @return 结果
     */
    @Override
    @Transactional
    public int insertRoomMeter(RoomMeter roomMeter) {
        int rows = 0;
        try {
            roomMeter.setRometerId(roomMeter.getRometerCode());
            roomMeter.setCreateBy(SecurityUtils.getUsername());
            roomMeter.setCreateTime(DateUtils.getNowDate());
            this.setUnitInfo(roomMeter);
            rows = roomMeterMapper.insertRoomMeter(roomMeter);
        } catch (Exception e) {
            if (e.getMessage().indexOf("for key 'PRIMARY'") != -1) {
                throw new BaseException("代码已存在！");
            } else {
                throw new BaseException(e.getMessage());
            }
        }
        return rows;
    }

    //房间抄表记录 工厂
//    private void copyRoomFactory(RoomMeter roomMeter) {
//        Room room = roomService.selectRoomById(roomMeter.getRoomId());
//        //添加成功后自动添加抄房间仪表
//        CopyRoom copyRoom = new CopyRoom();
//        copyRoom.setRometerId(roomMeter.getRometerId());//房间仪表ID
//        copyRoom.setOwnerId(room.getOwnerId());//业主ID
//        copyRoom.setLastTime(roomMeter.getLastTime());//上次读数日期
//        copyRoom.setLastReading(roomMeter.getLastReading());//上次读数
//        copyRoom.setReadingTime();//读数日期
//        copyRoom.setTheReading();//本次读数
//        copyRoom.setPracticalLine(); //实际行读
//        copyRoom.setAdjustLine() //调整行度
//        copyRoom.setRoomId(roomMeter.getRoomId());
//        copyRoomService.insertCopyRoom(copyRoom);
//    }


    /**
     * 修改房间仪表
     *
     * @param roomMeter 房间仪表
     * @return 结果
     */
    @Override
    public int updateRoomMeter(RoomMeter roomMeter) {
        this.setUnitInfo(roomMeter);
        roomMeter.setUpdateBy(SecurityUtils.getUsername());
        roomMeter.setUpdateTime(DateUtils.getNowDate());
        return roomMeterMapper.updateRoomMeter(roomMeter);
    }

    /**
     * 批量删除房间仪表
     *
     * @param rometerIds 需要删除的房间仪表ID
     * @return 结果
     */
    @Override
    public int deleteRoomMeterByIds(String[] rometerIds) {
        orgTreeService.deleteOrgTreeByIds(rometerIds);
        return roomMeterMapper.deleteRoomMeterByIds(rometerIds);
    }

    /**
     * 删除房间仪表信息
     *
     * @param rometerId 房间仪表ID
     * @return 结果
     */
    @Override
    public int deleteRoomMeterById(String rometerId) {
        orgTreeService.deleteOrgTreeById(rometerId);
        return roomMeterMapper.deleteRoomMeterById(rometerId);
    }


    private void setUnitInfo(RoomMeter bean) {
        Room room = roomService.selectRoomById(bean.getRoomId());
        bean.setAncestors(room.getAncestors() + "," + bean.getRoomId());
        bean.setDeptId(room.getDeptId());
        //orgTreeService.insertOrgTree(bean.getRometerId(),room.getOwnerVo().getOwnerName(), bean.getRometerId(), OrgConstants.rometer, room.getAncestors());
    }

    public String getNo(String key) {
        int nextVal = SerialNumUtil.getInstance().nextInt(key);
        return key + NumberUtils.frontCompWithZore(nextVal, 7);
    }
}
